﻿// Copyright (c) 2014 Morten Bakkedal
// This code is published under the MIT License.

using System;

using FuncLib;
using FuncLib.Functions;
using FuncLib.Mathematics;

namespace FuncLibSamples.Testing.ComplexFunctions
{
	public partial class ComplexFunction
	{
		public static ComplexFunction Sqrt(ComplexFunction f)
		{
			return new SqrtFunction(f);
		}

		[Serializable]
		private class SqrtFunction : ComplexFunction
		{
			private ComplexFunction f;

			public SqrtFunction(ComplexFunction f)
			{
				this.f = f;
			}

			protected override Complex ComputeValue(IEvaluator evaluation)
			{
				return Complex.Sqrt(f.ComputeValue(evaluation));
			}

			protected override ComplexFunction ComputeDerivative(ComplexVariable variable)
			{
				return 0.5 / this * f.Derivative(variable);
			}

			protected override Function Re()
			{
				return base.Re();
			}
		}
	}
}
